package global

import (
	"time"

	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

var Logger *zap.Logger

//设置日志记录器
func SetLogger() (err error) {
	config := zap.NewProductionConfig() //获得zap默认配置的对象
	level := Config.Section("log").Key("level").String()
	output := Config.Section("log").Key("output").String()
	encode := Config.Section("log").Key("encode").String()
	switch level {
	case "debug":
		config.Level.SetLevel(zap.DebugLevel)
	case "info":
		config.Level.SetLevel(zap.InfoLevel)
	case "warn":
		config.Level.SetLevel(zap.WarnLevel)
	case "error":
		config.Level.SetLevel(zap.ErrorLevel)
	default:
		config.Level.SetLevel(zap.InfoLevel)
	}
	if output != "" {
		config.OutputPaths = []string{output}
	}
	config.Encoding = encode
	config.EncoderConfig.CallerKey = "src"  //定义源码文件项的键名
	config.EncoderConfig.MessageKey = "msg" //定义消息项的键名
	config.EncoderConfig.TimeKey = "time"   //定义时间项的键名
	config.DisableStacktrace = true         //关闭堆栈跟踪信息
	//定义日期格式
	config.EncoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
		enc.AppendString(t.Format("2006-01-02 15:04:05"))
	}
	Logger, err = config.Build()
	if err != nil {
		return
	}
	return
}
